﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BCMS.Business.Implementation;
using BCMS.FrontEnd.DataContract;
using BCMS.Settings;
using System.IO;
using BCMS.Ultilities;

namespace BCMS.Forms
{
    public partial class frmUserAuthentication : Form
    {
        public frmUserAuthentication()
        {
            this.Font = SystemFonts.MessageBoxFont;
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                DALclient client = new DALclient();
                UserUI curUser = client.UserLogin(txtUserName.Text.Trim(), txtPassword.Text);
                if (curUser != null)
                {
                    GlobalVars.activeUser = curUser.UserName;
                    GlobalVars.activeRoles = curUser.Roles;
                    GlobalVars.activeUserId = curUser.ID;

                    if (chkSavePass.Checked)
                    {
                        string fileName = Application.StartupPath + "/usr.cnf";
                        if (File.Exists(fileName))
                            File.Delete(fileName);

                        string str = txtUserName.Text.Trim() + "\n" + txtPassword.Text;
                        str = Cryptor.Encrypt(str, "BCMS-20100430");

                        StreamWriter sw = new StreamWriter(fileName, false);
                        sw.Write(str);
                        sw.Flush();
                        sw.Close();
                    }
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    GlobalVars.ShowError("Tên đăng nhập hoặc mật khẩu không đúng.");
                    this.txtUserName.SelectAll();
                }
            }
            catch (Exception ex)
            {
                if (MessageBox.Show("Lỗi đăng nhập: " + ex.Message, "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error).Equals(DialogResult.Cancel))
                    Application.Exit();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void frmUserAuthentication_Load(object sender, EventArgs e)
        {
            try
            {
                string fileName = Application.StartupPath + "/usr.cnf";
                if (File.Exists(fileName))
                {
                    StreamReader sw = new StreamReader(fileName);
                    string str = sw.ReadLine();
                    sw.Close();
                    str = Cryptor.Decrypt(str, "BCMS-20100430");
                    string[] strs = str.Split('\n');
                    txtUserName.Text = strs[0];
                    txtPassword.Text = strs[1];
                    chkSavePass.Checked = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
            txtUserName.SelectAll();
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            frmSystemConfiguration config = new frmSystemConfiguration();
            config.ShowDialog();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                btnLogin_Click(null, null);
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
